// pages/login/login.js
import { request } from '../../request/index.js';
var app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		userInfo: {
			openid: '',
		},
		user: {},
		nickName: '',
		avatarUrl: '',
		phone: '',
		gg: [],
	},
	checkPhoneNum: function (phoneNumber) {
		let str = /^1\d{10}$/;
		if (str.test(phoneNumber)) {
			return true;
		} else {
			wx.showToast({
				title: '您输入的手机号不正确，请重新输入',
				icon: 'none',
			});
			return false;
		}
	},
	g(e) {
		if (e.detail.value.length === 11) {
			let checkedNum = this.checkPhoneNum(e.detail.value);
		}
		this.setData({
			phone: e.detail.value,
		});
	},
	login(e) {
		var that = this;
		var user = wx.getStorageSync('user');
		var u = wx.getStorageSync('u');
		if (this.data.phone.length < 11 || !this.checkPhoneNum(this.data.phone)) {
			wx.showToast({
				title: '您输入的手机号不足11位',
				icon: 'none',
			});
			return;
		}
		if (!this.data.phone) {
			wx.showToast({
				title: '您没有输入手机号，请输入后登录',
				icon: 'none',
			});
		} else {
			// if (!user) {
			if (that.data.gg.length == 0) {
				wx.getUserProfile({
					desc: '用于完善用户信息',
					success: r => {
						wx.setStorageSync('u', r.userInfo);
						that.setData({
							nickName: r.userInfo.nickName,
							avatarUrl: r.userInfo.avatarUrl,
						});
						// wx.uploadFile({
						//   url: 'https://www.chengkoo.cn/images/metal/addPic',
						//   filePath: that.avatarUrl,
						//   name: "file",
						//   success: function (res) {
						request({
							url: '/UserInfo/addUserInfo',
							data: {
								openid: that.data.userInfo.openid,
								nickName: that.data.nickName,
								userProfile: that.data.avatarUrl,
								userPhone1: that.data.phone,
							},
						}).then(a => {
							request({
								url: '/UserInfo/getAllUserInfoByType',
								data: {
									openid: that.data.userInfo.openid,
								},
							}).then(ss => {
								app.globalData.userId = ss.data.list[0].userId;
								wx.setStorageSync('user', ss.data.list[0]);
								that.setData({
									user: ss.data.list[0],
								});
								wx.switchTab({
									url: '/pages/index/index',
								});
							});
							//   })
							// }
						});
					},
					fail: d => {
						console.log(d);
					},
				});
			} else {
				app.globalData.userId = rs.data.list[0].userId;
				wx.setStorageSync('user', rs.data.list[0]);
				that.setData({
					user: rs.data.list[0],
				});
				wx.switchTab({
					url: '/pages/index/index',
				});
			}
			// } else {
			//   app.globalData.userId = user.userId;
			//   request({
			//     url: "/UserInfo/getUserInfo",
			//     data: {
			//       userId: user.userId
			//     }
			//   }).then(res => {
			//     that.setData({
			//       user: res.data
			//     })
			//     wx.switchTab({
			//       url: '/pages/index/index',
			//     })
			//   })

			// }
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (options.isshare == 1) {
			wx.navigateTo({
				url: '../guang/guang',
			});
		}
		var that = this;
		wx.login({
			success: function (res) {
				request({
					url: '/UserInfo/getCode',
					data: {
						code: res.code,
					},
				}).then(r => {
					var openid = 'userInfo.openid';
					that.setData({
						[openid]: r.data,
					});
					request({
						url: '/UserInfo/getAllUserInfoByType',
						data: {
							openid: that.data.userInfo.openid,
						},
					}).then(rs => {
						console.log(rs.data.list);
						that.setData({
							gg: rs.data.list,
						});
					});
				});
			},
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		return {
			path: '/pages/login/login?isshare=1',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			},
		};
	},
});
